<template>
	<a-modal :title="dialogType == 'add' ? '新增' : dialogType == 'edit' ? '编辑' : '详情'" :width="1200" :visible="visible"
		:maskClosable="false" :confirmLoading="confirmLoading" @ok="handleSubmit" @cancel="handleCancel">
		<a-spin :spinning="formLoading">
			<a-form :form="form">
				<a-form-item style="display: none;" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
					<a-input v-decorator="['id']" />
				</a-form-item>

				<a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
					<a-input placeholder="请输入" v-decorator="['name', { rules: [{ required: true, message: '请输入！' }] }]" />
				</a-form-item>

				<a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
					<a-input placeholder="请输入" :disabled="dialogType != 'add'"
						v-decorator="['code', { rules: [{ required: true, message: '请输入！' }] }]" />
				</a-form-item>

				<!-- <a-form-item
          label="上级机构"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-tree-select
            v-decorator="['pid', {rules: [{ required: true, message: '请选择上级机构！' }]}]"
            style="width: 100%"
            :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
            :treeData="orgTree"
            placeholder="请选择上级机构"
            treeDefaultExpandAll
          >
            <span slot="title" slot-scope="{ id }">{{ id }}</span>
          </a-tree-select>
        </a-form-item> -->

				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="机构类型">
					<a-radio-group v-decorator="['unitRanId', { rules: [{ required: true, message: '请选择！' }] }]"
						:disabled="dialogType === 'detail'">
						<a-radio :value="item.value" v-for="item in orgTypes" :key="item.value">{{ item.label }}</a-radio>
					</a-radio-group>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="物料类型">
					<a-radio-group v-decorator="['materialType', { rules: [{ required: true, message: '请选择！' }] }]"
						:disabled="dialogType === 'detail'">
						<a-radio :value="item.id" v-for="item in materialTypeList" :key="item.id">{{ item.name }}</a-radio>
					</a-radio-group>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否月结">
					<a-radio-group v-decorator="['isMonthSettlement', { rules: [{ required: true, message: '请选择！' }] }]"
						:disabled="dialogType === 'detail'">					
						<a-radio :value="2">是</a-radio>
						<a-radio :value="1">否</a-radio>
					</a-radio-group>
				</a-form-item>
				<!-- 如果是主要供应商才显示 -->
				<div>
					<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否需要开票">
						<a-radio-group v-decorator="['isInvoice', { rules: [{ required: true, message: '请选择！' }] }]"
							:disabled="dialogType === 'detail'">
							<a-radio :value="1">不需要</a-radio>
							<a-radio :value="2">需要</a-radio>
						</a-radio-group>
					</a-form-item>
					<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="form.getFieldValue('isInvoice') === 2"
						label="发票类型">
						<a-radio-group v-decorator="['invoiceType', { rules: [{ required: true, message: '请选择！' }] }]"
							:disabled="dialogType === 'detail'">
							<a-radio :value="1">普票</a-radio>
							<a-radio :value="2">专票</a-radio>
						</a-radio-group>
					</a-form-item>
					<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="税率(%)"
						v-if="form.getFieldValue('isInvoice') === 2">
						<a-input-number placeholder="请输入" style="width: 100%"
							v-decorator="['taxRate', { initialValue: undefined, rules: [{ required: true, message: '请输入！' }] }]"
							:min="0" :max="100" :precision="0" />
					</a-form-item>
				</div>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
					<a-input-number placeholder="请输入" style="width: 100%" v-decorator="['sort', { initialValue: undefined }]" />
				</a-form-item>
				<!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态"
        >
          <a-radio-group 
						v-decorator="['status', {rules: [{ required: true, message: '请选择！' }]}]"
            :disabled="dialogType === 'detail'"
					>
            <a-radio :value="0">正常</a-radio>
            <a-radio :value="1">停用</a-radio>
          </a-radio-group>
        </a-form-item> -->


			</a-form>

		</a-spin>
	</a-modal>
</template>

<script>
import {
	sysOrgAdd,
	sysOrgEdit,
	// getOrgTree 
} from '@/api/modular/system/orgManage'
import { sysDictDataPage } from '@/api/modular/system/dictDataManage'
export default {
	data() {
		return {
			dialogType: '',
			labelCol: {
				xs: { span: 24 },
				sm: { span: 5 }
			},
			wrapperCol: {
				xs: { span: 24 },
				sm: { span: 15 }
			},
			orgTree: [],
			visible: false,
			confirmLoading: false,
			formLoading: false,
			form: this.$form.createForm(this),
			orgTypes: [],
			materialTypeList: [
				this.$businessEnum.MATERIAL_TYPE_ENUM.material,
				this.$businessEnum.MATERIAL_TYPE_ENUM.customized,
				this.$businessEnum.MATERIAL_TYPE_ENUM.packing,
				this.$businessEnum.MATERIAL_TYPE_ENUM.cable,
			]
		}
	},
	methods: {
		// 初始化方法
		init(dialogType, record) {
			this.dialogType = dialogType
			this.visible = true
			// this.getOrgTree()
			this.getSupplierTypes()
			this.$nextTick(() => {
				// 新增的话设默认值 需要  普票
				if (dialogType == 'add') {
					this.form.setFieldsValue({
						unitRanId: '1440879787235438593'
					})
					setTimeout(() => {
						this.form.setFieldsValue(
							{
								isInvoice: 2,
								// status: 0,
							}
						)
						setTimeout(() => {
							this.form.setFieldsValue({
								invoiceType: 1,
							})
						}, 200)
					}, 200)
				}
				// 编辑和详情需要回显
				if (dialogType != 'add') {
					this.form.setFieldsValue({
						unitRanId: record.unitRanId,
					})
					setTimeout(() => {
						this.form.setFieldsValue(
							{
								id: record.id,
								name: record.name,
								code: record.code,
								sort: record.sort,
								pid: record.pid,
								isInvoice: record.isInvoice,
								remark: record.remark,
								materialType: record.materialType,
								isMonthSettlement: record.isMonthSettlement
							}
						)
						setTimeout(() => {
							this.form.setFieldsValue({
								invoiceType: record.invoiceType,
								taxRate: record.taxRate || undefined,
							})
						}, 200)
					}, 150)
				}
			})
		},

		/**
		 * 获取机构树，并加载于表单中
		 * 暂时不需要了
		 */
		// getOrgTree () {
		//   getOrgTree().then((res) => {
		//     this.formLoading = false
		//     if (!res.success) {
		//       this.orgTree = []
		//       return
		//     }
		//     this.orgTree = [{
		//       'id': '-1',
		//       'parentId': '0',
		//       'title': '顶级',
		//       'value': '0',
		//       'pid': '0',
		//       'children': res.data
		//     }]
		//   })
		// },
		/**
		 * 获取供应商类型，用于生成单选按钮
		 */
		getSupplierTypes() {
			sysDictDataPage({ typeId: '1440879091719172097', pageSize: 99, isSupplier: 1 }).then((res) => {
				let dictDataList = res.data.rows
				let orgTypes = dictDataList.map((dictData) => {
					return {
						value: dictData.id,
						label: dictData.value
					}
				})
				this.orgTypes = orgTypes
			})
		},

		handleSubmit() {
			let text = this.dialogType == 'edit' ? '编辑' : '新增'
			let api = this.dialogType == 'edit' ? sysOrgEdit : sysOrgAdd
			const { form: { validateFields } } = this
			this.confirmLoading = true
			validateFields((errors, values) => {
				if (!errors) {
					let query = {
						...values,
						isSupplier: 1,
						pid: '1473297518794452994',  //pid深祺电子写死
					}
					api(query).then((res) => {
						if (res.success) {
							this.$message.success(`${text}成功`)
							this.visible = false
							this.confirmLoading = false
							this.$emit('ok', values)
							this.form.resetFields()
						} else {
							this.$message.error(`${text}失败` + res.message)
						}
					}).finally((res) => {
						this.confirmLoading = false
					})
				} else {
					this.confirmLoading = false
				}
			})
		},
		handleCancel() {
			this.form.resetFields()
			this.visible = false
		}
	}
}
</script>
